<template>
    <div id="app">
        <router-view />
    </div>
</template>

<script>
import * as Auth from "@/utils/auth";
import Vue from "vue";
import { getUserInfoById } from "@/api/system/user";

export default {
    name: "App",
    data() {
        return {
            timer: null,
            taskNotifyShow: false,
        };
    },
    mounted() {
        this.setUserId();
    },
    metaInfo() {
        return {
            title: this.$store.state.settings.dynamicTitle && this.$store.state.settings.title,
            titleTemplate: title => {
                return title ? `${title} - ${process.env.VUE_APP_TITLE}` : process.env.VUE_APP_TITLE;
            },
        };
    },
    methods: {
        setUserId() {
            // 获取当前页面的URL
            var url = window.location.href;
            // 使用正则表达式匹配userId参数及其值
            var match = url.match(/[?&]userId=([^&]+)/);
            // 如果匹配成功，则提取userId的值
            var userId;
            if (match) {
                userId = decodeURIComponent(match[1]);
            } else {
                userId = Auth.getUserId();
            }
            if (userId) {
                Auth.setUserId(userId);
                let userInfo = Auth.getUserInfo();
                if (userInfo == null || userInfo == undefined) {
                    this.getUserInfo(userId);
                } else {
                    if (userInfo.userId != userId) {
                        this.getUserInfo(userId);
                    }
                }
            }
            // 连接websocket
            this.initWebSocket(userId);
        },
        getUserInfo(userId) {
            getUserInfoById(userId).then(res => {
                console.log(res);
                Auth.setUserInfo(res.data);
            });
        },
        initWebSocket(userId) {
            console.log(userId, "websocket");
            // 登录状态，启动IM
            this.$chat.init(userId).then(() => {
                this.$chat.setGlobalListener(data => {
                    this.onChatMessage(data);
                });
                this.$chat.open();
            });
        },
        onChatMessage(data) {
            console.log("收到消息", data);
        },
    },
};
</script>

<style>
.el-input--medium .el-input__inner {
    padding-right: 15px;
}

.el-date-editor .el-input__inner {
    padding-right: 0;
}

.el-date-editor.el-input {
    width: 100%;
}

.el-form-item--medium .el-form-item__label {
    width: 120px;
    height: 36px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
</style>
